package Sort;

import java.util.ArrayDeque;

public class Sort {
    // 插入排序
    //时间复杂度：O（N2）
    //最好O（N）
    public static void insertSort(int[] array) {
       int tmp = 0;
        for (int i = 1; i < array.length; i++) {
            tmp = array[i];//先把待移动的拿走
            int j = i-1;
            while (j>=0&&array[j]>tmp){
                array[j+1] = array[j];//向前移动 腾出位置
                j--;//找到待交换的
            }
            array[j+1] = tmp;
        }
    }
        // 希尔排序
       public static void shellSort(int[] array){
        int gap = array.length/2;
        while (gap>1){
            shell(array,gap);
            gap/=2;
        }
    }

    private static void shell(int[] array, int gap) {
        int i, j, tmp;
        for (i = gap; i < array.length; i++) {
            tmp = array[i];  // 在外层循环保存当前待插入元素
            j = i - gap;
            while (j >= 0 && array[j] > tmp) {
                array[j + gap] = array[j];  // 元素后移
                j-=gap;
            }
            array[j + gap] = tmp;  // 插入到正确位置
        }
    }

    //选择排序

    public static void SelectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j]<array[minIndex]){
                   minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }
    //堆排序
    /*思路：
    * 1.要建立一个大根堆，
    * 2.把堆顶元素换到最后一个位置，将堆顶向下调整 此时要注意，向下调整的范围应当排除最后一个位置
    * 3.重复上述操作，直至最后一个元素*/
    public static void heapSort(int[] array){
        int size = array.length;
        creatHeap(array,size);
        for (int i = size-1; i > 0; i--) {
            swap(array,i,0);
            shiftDown(array,0,i);
        }
    }

    private static void creatHeap(int[] array, int size) {
        for (int i = (size-1-1)/2; i >=0 ; i--) {
            shiftDown(array,i,size);
        }
    }

    private static void shiftDown(int[] array,int parent, int size) {
        int child = parent*2 + 1;
        while (child<size){
            if(child+1<size&&array[child+1]>array[child]){
                child++;
            }
            if(array[child]>array[parent]){
                swap(array,child,parent);
                parent = child;
                child = parent*2 + 1;
            }else {
                break;
            }

        }
    }

    //冒泡排序
    //可以使用flg来提高效率
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if(flg == false){
              return;
             }
        }
    }
    
    //快速排序 挖坑法
    public static void quickSort(int[] array) {
        quickNor(array,0,array.length-1);
        //quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int start, int end) {
        if(start>=end){
            return;
        }
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }
    private static void quickNor(int[] array, int left, int right){
        int pivot =  partition(array,left,right);
        ArrayDeque<Integer> stack = new ArrayDeque<>();

        //这里的判断很重要   至少要有两个元素才入栈 不然无意义!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        if(pivot > left + 1) {
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot < right-1) {
            stack.push(pivot+1);
            stack.push(right);
        }
        while (!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            pivot =  partition(array,left,right);
            if(pivot > left + 1) {
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot < right-1) {
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }
    //如果是挖坑法 不必保留start
    private static int partition(int[] array, int left, int right) {
//        int left = start;
//        int right = end;
        int tmp = array[left];
        while (left<right){
            while (left<right && array[right]>=tmp){//要加等号！！！！！！！！！！！！！！！！！！！！！！！！！
                right--;
            }
             array[left]=array[right];
            while (left<right && array[left]<=tmp){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
  //归并排序
    /*
    * 递归进行拆分 在每一段都拆分完返回的栈帧下进行合并*/
    public static void mergeSort(int[] array){
        int left = 0;
        int right = array.length - 1;
        //mergeSortTmp(array,left,right);
        mergeSortNor(array,left,right);
    }

    private static void mergeSortNor(int[] array, int left, int right) {
        int gap = 1;
        while (gap<array.length){
            for (int i = left; i < array.length; i+=gap*2) {
                int start = i;

                int mid = start + gap -1;
                if(mid>right){
                    mid = right;
                }
                int end = mid + gap;
                if(end>right){
                    end = right;
                }
                merge(array,start,mid,end);
            }

            gap*=2;
        }

    }

    private static void mergeSortTmp(int[] array, int left, int right) {
        if(left>=right){
            return;
        }
        int mid = (left+right)/2;
        mergeSortTmp(array,left,mid);
        mergeSortTmp(array,mid+1,right);
        //走到此处 代表已经分解完毕 此时的返回 对应分好组的上一层
        merge(array,left,mid,right);
    }
    

    private static void merge(int[] array, int left, int mid, int right) {
        int[] tmp = new int[right - left+1];int len = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;

        while (s1<=e1 && s2<=e2){
            if(array[s1]<=array[s2]){
                tmp[len++] = array[s1++];
            }

            if(array[s2]<array[s1]){
                tmp[len++] = array[s2++];
            }
        }
        while (s1<=e1){
            tmp[len++] = array[s1++];
        }
        while (s2<=e2){
            tmp[len++] = array[s2++];
        }

        for (int i = 0; i < len; i++) {
            array[i+left] = tmp[i];
        }
    }


    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }


}
